package com.jh3502.vivo.function.main;

import android.os.Bundle;
import android.support.v7.widget.StaggeredGridLayoutManager;

import com.jh3502.vivo.R;
import com.jh3502.vivo.base.BaseFragment;
import com.jh3502.vivo.function.author.AuthorListActivity;
import com.jh3502.vivo.function.reimburse.ReimburseListActivity;
import com.jh3502.vivo.function.todo.TodoListActivity;
import com.jh3502.vivo.function.travel.TravelListActivity;
import com.jh3502.vivo.utils.Utils;
import com.jh3502.vivo.view.MyRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * author:harisucici on 2018/11/7 10:19
 * email:harisucici@gmail.com
 * name:
 * todo:
 */
public class MainFragment extends BaseFragment<MainPresenter.MainView, MainPresenter> implements MainPresenter.MainView {

    @BindView(R.id.main_menu)
    MyRecyclerView mainMenu;
    private MainAdapter mainAdapter;
    private int[] images = {R.drawable.ic_upcoming, R.drawable.ic_travel,
            R.drawable.ic_reimburse, R.drawable.ic_delegation};
    private List<TitleBean> titles = new ArrayList<>(2);

    @Override
    public MainPresenter initPresenter() {
        return new MainPresenter();
    }

    @Override
    protected int setViewId() {
        return R.layout.fragment_main;
    }

    @Override
    protected void initView() {

        initMenu();
        StaggeredGridLayoutManager menu = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        mainAdapter = new MainAdapter(mContext, titles);

        mainMenu.setLayoutManager(menu);
        mainMenu.setAdapter(mainAdapter);
        mainAdapter.setOnItemClickListener(s -> {
            if (s == 0) {
                Utils.getInstance().startActivity(mActivity, TodoListActivity.class);
            } else if (s == 1) {
                Utils.getInstance().startActivity(mActivity, TravelListActivity.class);
            } else if (s == 2) {
                Utils.getInstance().startActivity(mActivity, ReimburseListActivity.class);
            } else if (s == 3) {
                Utils.getInstance().startActivity(mActivity, AuthorListActivity.class);
            }
        });

        mPresenter.setMainView(this);

    }

    private void initMenu() {
        String[] title = getResources().getStringArray(R.array.title);
        for (int i = 0; i < title.length; i++) {
            TitleBean titleBean = new TitleBean();
            titleBean.setTitle(title[i]);
            titleBean.setImageId(images[i]);
            titles.add(titleBean);
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void doOnDestroy() {

    }

    @Override
    protected void doOnResume() {
//        mPresenter.toFindTdNum();

    }

    @Override
    protected void doOnVisible() {
//        mPresenter.toFindTdNum();

    }

    @Override
    protected void doOnInvisible() {

    }

    @Override
    protected void doOnSaveInstanceState(Bundle savedInstanceState) {

    }

    @Override
    public void responseError(int code, String msg) {
        showShortToast(msg);
    }

    @Override
    public void requestFailure(Throwable t) {

    }

    @Override
    public void findTdNumSuccess(int num) {
        showShortToast("更新成功");
        mainAdapter.setPoint(0, num);
        mainAdapter.notifyDataSetChanged();
    }
}
